<template>
  <div
    :ref="refName"
    :optionData="optionData"
    :title="title"
    :dark="dark"
    :top="top"
    :right="right"
    :x="x"
    :y="y"
    :flag="flag"
    :interval="interval"
    style="width: 100%; height: 100%"
  ></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  props: {
    refName: {
      type: String,
      default: "ref",
    },
    optionData: {
      type: Object,
    },
    dark: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "",
    },
    top: {
      type: String,
      default: "0",
    },
    right: {
      type: String,
      default: "0",
    },
    x: {
      type: Number,
      default: 35,
    },
    y: {
      type: Number,
      default: 40,
    },
    interval: {
      type: Number,
      default: 40,
    },
    flag: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      chartData: null,
      myChart: null,
    };
  },
  watch: {
    optionData: {
      handler(newVal, oldVal) {
        this.chartData = newVal;
        this.init();
      },
      deep: true,
    },
  },
  methods: {
    formatData(data) {
      let res = [];
      if (data.length > 0) {
        for (let item of data) {
          let obj = {
            value: item.count,
            name: `${item.starLevel}\t\t${item.comparedToLastWeek}\n${item.count}个\t\t\t${item.proportion}`,
          };
          res.push(obj);
        }
      }
      return res;
    },
    init() {
      if (this.myChart) {
        this.myChart.clear();
        this.myChart.dispose();
        this.myChart = null;
      }
      this.myChart = echarts.init(this.$refs[this.refName]);
      let option = {
        title: [
          {
            show: true,
            text: `${this.chartData.count}`,
            left:
              this.chartData.count > 99
                ? "25%"
                : this.chartData.count > 9
                ? "28%"
                : "31%",
            top: "30%",
            textStyle: {
              fontWeight: 500,
              fontSize: 36,
              color: "#2087D0",
            },
          },
          {
            show: true,
            text: "总数",
            left: "28.5%",
            top: "41%",
            textStyle: {
              fontWeight: 400,
              fontSize: 18,
              color: "#666",
            },
          },
        ],
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          top: this.top,
          right: this.right,
          itemGap: this.interval,
        },
        series: [
          {
            type: "pie",
            radius: ["40%", "60%"],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: "center",
              color: "inherit",
            },
            center: [`${this.x}%`, `${this.y}%`],
            labelLine: {
              show: false,
            },
            data: this.flag
              ? this.chartData.list
              : this.formatData(this.chartData.echartsList),
          },
        ],
      };
      option && this.myChart.setOption(option);
    },
  },
};
</script>
